﻿using Microsoft.EntityFrameworkCore;
using Niti.OrderManage.OrderManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using static Niti.OrderManage.NitiOrderManageDbConsts;

namespace Niti.OrderManage
{
   public static class NitiOrderManageDbContextModelCreatingExtensions
    {
        public static void Configure(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            #region 配置表模型，包括表名、字段类型和长度等信息
            builder.Entity<Commodity>(b =>
            {
                b.ToTable(NitiOrderManageConsts.DbTablePrefix + DbTableName.Commoditys);
                b.HasKey(x => x.Id);
                b.Property(x => x.Code).HasMaxLength(255).IsRequired();
                b.Property(x => x.CName).HasMaxLength(200).IsRequired();
                b.Property(x => x.CPrice).HasColumnType("float");
                b.Property(x => x.CreationTime).HasColumnType("datetime ");
                b.Property(x => x.UpdateTime).HasColumnType("datetime ");
                b.Property(x => x.DeleteTime).HasColumnType("datetime ");
                b.Property(x => x.Count).HasColumnType("int ");
                b.Property(x => x.IsDeleted).HasColumnType("bit").HasDefaultValue(false);
            });
            builder.Entity<Supplier>(b =>
            {
                b.ToTable(NitiOrderManageConsts.DbTablePrefix + DbTableName.Suppliers);
                b.HasKey(x => x.Id);
                b.Property(x => x.Code).HasMaxLength(255).IsRequired();
                b.Property(x => x.ContactName).HasMaxLength(100);
                b.Property(x => x.ContactNumber).HasMaxLength(20);
                b.Property(x => x.SName).HasMaxLength(200);
                b.Property(x => x.BeginTime).HasColumnType("date");//HasMaxLength(20);
                b.Property(x => x.EndTime).HasColumnType("date");//.HasMaxLength(20);
                b.Property(x => x.CreationTime).HasColumnType("datetime ");
                b.Property(x => x.UpdateTime).HasColumnType("datetime ");
                b.Property(x => x.DeleteTime).HasColumnType("datetime ");
                b.Property(x => x.IsDeleted).HasColumnType("bit").HasDefaultValue(false);
            });
            builder.Entity<Order>(b =>
            {
                b.ToTable(NitiOrderManageConsts.DbTablePrefix + DbTableName.Orders);
                b.HasKey(x => x.Id);
                b.Property(x => x.Code).HasMaxLength(200).IsRequired();
                b.Property(x => x.Title).HasMaxLength(200).IsRequired();
                b.Property(x => x.Supplier_Code).HasMaxLength(200).IsRequired();
                b.Property(x => x.Supplier_SName).HasMaxLength(200);
                b.Property(x => x.Count).HasColumnType("int").IsRequired();
                b.Property(x => x.TotalPrice).HasColumnType("float").IsRequired();
                b.Property(x => x.CreationTime).HasColumnType("datetime ");
                b.Property(x => x.UpdateTime).HasColumnType("datetime ");
                b.Property(x => x.CheckTime).HasColumnType("datetime ");
                //b.Property(x => x.DeleteTime).HasColumnType("datetime ");
                b.Property(x => x.IsDraft).HasColumnType("int ");
                //b.Property(x => x.IsDeleted).HasColumnType("bit").HasDefaultValue(false);
                
            });
            builder.Entity<Order>().Ignore(b=>b.OrderLineItem);
            builder.Entity<OrderLineItem>(b =>
            {
                b.ToTable(NitiOrderManageConsts.DbTablePrefix + DbTableName.OrderLineItems);
                b.HasKey(x => x.Id);
                b.Property(x => x.OrderCode).HasMaxLength(200).IsRequired();
                b.Property(x => x.LineNumber).HasColumnType("int").IsRequired();
                b.Property(x => x.Commodity_Code).HasMaxLength(200).IsRequired();
                b.Property(x => x.Commodity_CName).HasMaxLength(200).IsRequired();
                b.Property(x => x.Commodity_CPrice).HasColumnType("float").IsRequired();
                b.Property(x => x.Count).HasColumnType("int").IsRequired();
                b.Property(x => x.Subtotal).HasColumnType("float").IsRequired();
                b.Property(x => x.CreationTime).HasColumnType("datetime ");
                b.Property(x => x.UpdateTime).HasColumnType("datetime ");
               // b.HasOne<Order>().WithMany().HasForeignKey(x => x.OrderCode).IsRequired();
            });
            builder.Entity<ReceiptOrder>(b =>
            {
                b.ToTable(NitiOrderManageConsts.DbTablePrefix + DbTableName.ReceiptOrders);
                b.HasKey(x => x.Id);
                b.Property(x => x.Code).HasMaxLength(200).IsRequired();
                b.Property(x => x.ReceiptCode).HasMaxLength(200).IsRequired();
                b.Property(x => x.Title).HasMaxLength(200).IsRequired();
                b.Property(x => x.Supplier_Code).HasMaxLength(200).IsRequired();
                b.Property(x => x.Supplier_SName).HasMaxLength(200);
                b.Property(x => x.Count).HasColumnType("int").IsRequired();
                b.Property(x => x.TotalPrice).HasColumnType("float").IsRequired();
                b.Property(x => x.RealityCount).HasColumnType("int");
                b.Property(x => x.RealityTotalPrice).HasColumnType("float");
                b.Property(x => x.CreationTime).HasColumnType("datetime ");
                b.Property(x => x.UpdateTime).HasColumnType("datetime ");
                b.Property(x => x.CheckTime).HasColumnType("datetime ");
                //b.Property(x => x.DeleteTime).HasColumnType("datetime ");
                b.Property(x => x.IsDraft).HasColumnType("int ");
                //b.Property(x => x.IsDeleted).HasColumnType("bit").HasDefaultValue(false);

            });
            builder.Entity<ReceiptOrder>().Ignore(b => b.ReceiptOrderLineItem);
            builder.Entity<ReceiptOrderLineItem>(b =>
            {
                b.ToTable(NitiOrderManageConsts.DbTablePrefix + DbTableName.ReceiptOrderLineItems);
                b.HasKey(x => x.Id);
                b.Property(x => x.ReceiptOrderCode).HasMaxLength(200).IsRequired();
                b.Property(x => x.LineNumber).HasColumnType("int").IsRequired();
                b.Property(x => x.Commodity_Code).HasMaxLength(200).IsRequired();
                b.Property(x => x.Commodity_CName).HasMaxLength(200).IsRequired();
                b.Property(x => x.Commodity_CPrice).HasColumnType("float").IsRequired();
                b.Property(x => x.RealityCPrice).HasColumnType("float").IsRequired();
                b.Property(x => x.Count).HasColumnType("int").IsRequired();
                b.Property(x => x.Subtotal).HasColumnType("float").IsRequired();
                b.Property(x => x.RealityCount).HasColumnType("int").IsRequired();
                b.Property(x => x.RealitySubtotal).HasColumnType("float").IsRequired();
                b.Property(x => x.CreationTime).HasColumnType("datetime ");
                b.Property(x => x.UpdateTime).HasColumnType("datetime ");
                // b.HasOne<Order>().WithMany().HasForeignKey(x => x.OrderCode).IsRequired();
            });
            builder.Entity<CheckOutOrder>(b =>
            {
                b.ToTable(NitiOrderManageConsts.DbTablePrefix + DbTableName.CheckOutOrders);
                b.HasKey(x => x.Id);
                b.Property(x => x.Code).HasMaxLength(200).IsRequired();
                b.Property(x => x.ApplyUser).HasMaxLength(200).IsRequired();
                b.Property(x => x.Description);
                b.Property(x => x.Count).HasColumnType("int").IsRequired();
                b.Property(x => x.TotalPrice).HasColumnType("float").IsRequired();
                b.Property(x => x.CreationTime).HasColumnType("datetime ");
                b.Property(x => x.UpdateTime).HasColumnType("datetime ");
                b.Property(x => x.CheckTime).HasColumnType("datetime ");
                b.Property(x => x.IsDraft).HasColumnType("int ");

            });
            builder.Entity<CheckOutOrder>().Ignore(b => b.CheckOutOrderLineItem);
            builder.Entity<CheckOutOrderLineItem>(b =>
            {
                b.ToTable(NitiOrderManageConsts.DbTablePrefix + DbTableName.CheckOutOrderLineItems);
                b.HasKey(x => x.Id);
                b.Property(x => x.CheckOutOrderCode).HasMaxLength(200).IsRequired();
                b.Property(x => x.LineNumber).HasColumnType("int").IsRequired();
                b.Property(x => x.Commodity_Code).HasMaxLength(200).IsRequired();
                b.Property(x => x.Commodity_CName).HasMaxLength(200).IsRequired();
                b.Property(x => x.Commodity_CPrice).HasColumnType("float").IsRequired();
                b.Property(x => x.Count).HasColumnType("int").IsRequired();
                b.Property(x => x.Subtotal).HasColumnType("float").IsRequired();
                b.Property(x => x.CreationTime).HasColumnType("datetime ");
                b.Property(x => x.UpdateTime).HasColumnType("datetime ");
            });
            #endregion
        }
    }
}
